package app.Disthanitian.thaistyle.EmerMez;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class MyPaper extends Activity {
	private Socket socket = null;
	private DataInputStream console = null;
	private DataOutputStream streamOut = null;
	private MyPaperThread client = null;

	private Handler handler;
	public static float[] ipointsx = new float[30000];
	public static float[] ipointsy = new float[30000];
	public int countpoint = 0;
	float mx = 0, my = 0;
	DrawView drawView;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		drawView = new DrawView(this);
		setContentView(drawView);
		connect("192.168.2.44", 9999);

	}

	public void connect(String serverName, int serverPort) {
		showMsg("Establishing connection. Please wait...");
		try {
			socket = new Socket(serverName, serverPort);
			showMsg("Connected :" + socket);
			open();
		} catch (UnknownHostException uhe) {
			println("Host Unknown:" + uhe.getMessage());
		} catch (IOException ioe) {
			println("Unexcepted exception" + ioe.getMessage());

		}

	}

	private void send(String text) {
		if (text != "") {
			try {
				streamOut.writeUTF(text);
				streamOut.flush();
			} catch (IOException ioe) {
				println("Sending error:" + ioe.getMessage());
				close();
			}

		}
	}

	public String massage = null;

	public void handle(String msg) {
		massage = msg;
		if (msg.equals(".bye")) {
			close();
		} else {
			String p[] = msg.split(":");
			Log.v("msg", "msg=" + msg);
			String point[] = p[1].split(",");
			Log.v("msg", "msg=" + msg);
			Point po = new Point();
			mx = Float.parseFloat(point[0]);
			my = Float.parseFloat(point[1]);

			ipointsx[countpoint] = mx;
			ipointsy[countpoint] = my;
			countpoint++;
			SystemClock.sleep(1000);
		}

	}

	private void open() {
		try {
			streamOut = new DataOutputStream(socket.getOutputStream());
			client = new MyPaperThread(this, socket);
			try {
				streamOut.writeUTF("#TestName");
				streamOut.flush();
			} catch (IOException ioe) {
				println("Sending error:" + ioe.getMessage());
				close();
			}
		} catch (IOException ioe) {
			println("Error opening output stream:" + ioe);
		}

	}

	public void close() {
		try {
			if (streamOut != null)
				streamOut.close();
			if (socket != null)
				socket.close();
		} catch (IOException ioe) {
			println("Error closing...");
		}
		client.close();
		client.stop();
	}

	private void println(String msg) {

	}

	private void showMsg(String msg) {
		Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();

	}

	class DrawView extends View implements OnTouchListener {
		private static final String TAG = "DrawView";
		List<Point> points = new ArrayList<Point>();
		Paint paint = new Paint();

		public DrawView(Context context) {
			super(context);
			setFocusable(true);
			setFocusableInTouchMode(true);
			this.setOnTouchListener(this);
			paint.setColor(Color.BLUE);
			paint.setAntiAlias(true);
		}

		public void onDraw(Canvas canvas) {
			// for (Point point : points) {
			// canvas.drawCircle(point.x, point.y, 5, paint);
			// }
			for (int i = 0; i < countpoint; i++) {
				canvas.drawCircle(ipointsx[i], ipointsy[i], 5, paint);
			}
			invalidate();
		}

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			Point point = new Point();
			point.x = event.getX();
			point.y = event.getY();
			points.add(point);
			invalidate();
			Log.d(TAG, "point:" + point);
			return true;
		}

	}

	class Point {
		float x, y;

		public String toString() {
			return x + "," + y;
		}
	}

}
